package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.Recommend;
import com.example.result.Result;
import com.example.service.RecommendService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 就业推荐控制层
 * @date 2020-02-20
 */
@RestController
@RequestMapping("/api")
public class RecommendController {

    @Autowired
    RecommendService recommendService;

    // ===============================就业推荐管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加就业推荐信息
     */
    @RequestMapping(value = "/recommends", method = RequestMethod.POST)
    public Result addRecommend(@RequestBody String json) {
        // 将JSON数据转换为Recommend对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Recommend recommend = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Recommend.class);

        return recommendService.addRecommend(recommend);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新就业推荐信息
     */
    @RequestMapping(value = "/recommends", method = RequestMethod.PUT)
    public Result updateRecommend(@RequestBody String json) {
        // 将JSON数据转换为Recommend对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Recommend recommend = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Recommend.class);
        return recommendService.updateRecommend(recommend);

    }

    /**
     * @param recommendId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据就业推荐编号删除就业推荐信息
     */
    @RequestMapping(value = "/recommends/{recommendId}", method = RequestMethod.DELETE)
    public Result deleteRecommendById(@PathVariable("recommendId") String recommendId) {
        return recommendService.deleteRecommendById(recommendId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有就业推荐信息
     */
    @RequestMapping(value = "/recommends", method = RequestMethod.GET)
    public Result queryAllRecommend() {
        return recommendService.queryAllRecommend();

    }

    /**
     * @param recommendId
     * @return 以JSON格式返回该就业推荐信息
     * @describe 根据编号查询就业推荐信息
     */
    @RequestMapping(value = "/recommends/{recommendId}", method = RequestMethod.GET)
    public Result queryRecommendById(@PathVariable("recommendId") String recommendId) {
        return recommendService.queryRecommendById(recommendId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加就业推荐信息
     */
    @RequestMapping(value = "/recommends/excels", method = RequestMethod.POST)
    public Result addRecommendByExcel(@RequestBody String json) {

        JSONObject parseObject = JSON.parseObject(json);
        List<Recommend> list = JSON.parseArray(parseObject.getString("json"), Recommend.class);

        return recommendService.addRecommendByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据就业推荐编号批量删除就业推荐信息
     */
    @RequestMapping(value = "/recommends/dels", method = RequestMethod.POST)
    public Result deleteRecommendByIds(@RequestBody Map<String, String> Ids) {

        return recommendService.deleteRecommendByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 就业推荐信息模糊查询
     */
    @RequestMapping(value = "/recommends/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return recommendService.serachByInfo(info);
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有推荐以及教师信息
     */
    @RequestMapping(value = "/recommends/recommendAndUsers", method = RequestMethod.GET)
    public Result queryAllRecommendAndUsersInfo() {
        return recommendService.queryAllRecommendAndUsersInfo();
    }
}
